
/*
 * Copyright 2002-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*
 *版权所有2002-2018原作者。
 *
 *根据Apache许可证2.0版（“许可证”）获得许可；
 *除非符合许可证，否则不得使用此文件。
 *您可以在以下地址获取许可证副本：
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 *除非适用法律要求或书面同意，软件
 *根据许可证分发的内容是按“原样”分发的，
 *无任何明示或暗示的保证或条件。
 *有关管理权限和
 *许可证下的限制。
 */

package org.springframework.aop.support;

import java.io.Serializable;

import org.aopalliance.aop.Advice;

import org.springframework.aop.Pointcut;
import org.springframework.lang.Nullable;
import org.springframework.util.ObjectUtils;

/**
 * Convenient class for regexp method pointcuts that hold an Advice,
 * making them an {@link org.springframework.aop.Advisor}.
 *
 * <p>Configure this class using the "pattern" and "patterns"
 * pass-through properties. These are analogous to the pattern
 * and patterns properties of {@link AbstractRegexpMethodPointcut}.
 *
 * <p>Can delegate to any {@link AbstractRegexpMethodPointcut} subclass.
 * By default, {@link JdkRegexpMethodPointcut} will be used. To choose
 * a specific one, override the {@link #createPointcut} method.
 *
 * @author Rod Johnson
 * @author Juergen Hoeller
 * @see #setPattern
 * @see #setPatterns
 * @see JdkRegexpMethodPointcut
 */
/**
 *用于保存Advice的regexp方法切入点的方便类，
 *使它们成为一个{@link.org.springframework.aop.Advisor}。
 *
 *＜p＞使用“pattern”和“pattern”配置该类
 *直通财产。这些与模式类似
 *和模式{@link AbstractRegexpMethodPointcut}的财产。
 *
 *＜p＞可以委托给任何｛@link AbstractRegexpMethodPointcut｝子类。
 *默认情况下，将使用｛@link JdkRegexpMethodPointcut｝。选择
 *一个特定的方法，重写｛@link#createPointcut｝方法。
 *
 *@作者Rod Johnson
 *@作者Juergen Hoeller
 *@参见#setPattern
 *@参见#setPatterns
 *@参见JdkRegexpMethodPointcut
 */
@SuppressWarnings("serial")
public class RegexpMethodPointcutAdvisor extends AbstractGenericPointcutAdvisor {

	@Nullable
	private String[] patterns;

	@Nullable
	private AbstractRegexpMethodPointcut pointcut;

	private final Object pointcutMonitor = new SerializableMonitor();


	/**
	 * Create an empty RegexpMethodPointcutAdvisor.
	 * @see #setPattern
	 * @see #setPatterns
	 * @see #setAdvice
	 */
	/**
	 *创建一个空的RegexpMethodPointcutAdvisor。
	 *@参见#setPattern
	 *@参见#setPatterns
	 *@参见#setAdvice
	 */
	public RegexpMethodPointcutAdvisor() {
	}

	/**
	 * Create a RegexpMethodPointcutAdvisor for the given advice.
	 * The pattern still needs to be specified afterwards.
	 * @param advice the advice to use
	 * @see #setPattern
	 * @see #setPatterns
	 */
	/**
	 *为给定的建议创建RegexpMethodPointcutAdvisor。
	 *之后仍需要指定模式。
	 *@param建议要使用的建议
	 *@参见#setPattern
	 *@参见#setPatterns
	 */
	public RegexpMethodPointcutAdvisor(Advice advice) {
		setAdvice(advice);
	}

	/**
	 * Create a RegexpMethodPointcutAdvisor for the given advice.
	 * @param pattern the pattern to use
	 * @param advice the advice to use
	 */
	/**
	 *为给定的建议创建RegexpMethodPointcutAdvisor。
	 *@param pattern要使用的模式
	 *@param建议要使用的建议
	 */
	public RegexpMethodPointcutAdvisor(String pattern, Advice advice) {
		setPattern(pattern);
		setAdvice(advice);
	}

	/**
	 * Create a RegexpMethodPointcutAdvisor for the given advice.
	 * @param patterns the patterns to use
	 * @param advice the advice to use
	 */
	/**
	 *为给定的建议创建RegexpMethodPointcutAdvisor。
	 *@param模式要使用的模式
	 *@param建议要使用的建议
	 */
	public RegexpMethodPointcutAdvisor(String[] patterns, Advice advice) {
		setPatterns(patterns);
		setAdvice(advice);
	}


	/**
	 * Set the regular expression defining methods to match.
	 * <p>Use either this method or {@link #setPatterns}, not both.
	 * @see #setPatterns
	 */
	/**
	 *设置要匹配的正则表达式定义方法。
	 *＜p＞使用此方法或｛@link#setPatterns｝，而不是两者都使用。
	 *@参见#setPatterns
	 */
	public void setPattern(String pattern) {
		setPatterns(pattern);
	}

	/**
	 * Set the regular expressions defining methods to match.
	 * To be passed through to the pointcut implementation.
	 * <p>Matching will be the union of all these; if any of the
	 * patterns matches, the pointcut matches.
	 * @see AbstractRegexpMethodPointcut#setPatterns
	 */
	/**
	 *将定义方法的正则表达式设置为匹配。
	 *传递给切入点实现。
	 *＜p＞匹配将是所有这些的结合；如果
	 *模式匹配，切入点匹配。
	 *@参见AbstractRegexpMethodPointcut#setPatterns
	 */
	public void setPatterns(String... patterns) {
		this.patterns = patterns;
	}


	/**
	 * Initialize the singleton Pointcut held within this Advisor.
	 */
	/**
	 *初始化此Advisor中保存的单例Pointcut。
	 */
	@Override
	public Pointcut getPointcut() {
		synchronized (this.pointcutMonitor) {
			if (this.pointcut == null) {
				this.pointcut = createPointcut();
				if (this.patterns != null) {
					this.pointcut.setPatterns(this.patterns);
				}
			}
			return this.pointcut;
		}
	}

	/**
	 * Create the actual pointcut: By default, a {@link JdkRegexpMethodPointcut}
	 * will be used.
	 * @return the Pointcut instance (never {@code null})
	 */
	/**
	 *创建实际切入点：默认情况下，｛@link JdkRegexpMethodPointcut｝
	 *将使用。
	 *@return Pointcut实例（从不｛@code null｝）
	 */
	protected AbstractRegexpMethodPointcut createPointcut() {
		return new JdkRegexpMethodPointcut();
	}

	@Override
	public String toString() {
		return getClass().getName() + ": advice [" + getAdvice() +
				"], pointcut patterns " + ObjectUtils.nullSafeToString(this.patterns);
	}


	/**
	 * Empty class used for a serializable monitor object.
	 */
	/**
	 *用于可序列化监视器对象的空类。
	 */
	private static class SerializableMonitor implements Serializable {
	}

}
